package org.apache.ignite.examples.datagrid;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.IgniteException;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.CacheMode;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.examples.model.Stock;

import java.util.List;

/**
 * Created by xbkaishui on 16/8/7.
 */
public class TestSqlQuery {

	public static void main(String[] args) throws IgniteException {
		String CACHE_NAME = "KLINE_DAY_CACHE";

		try (Ignite ignite = Ignition.start("config/example-ignite.xml")) {

			CacheConfiguration<String, Stock> stockCfg = new CacheConfiguration<>(CACHE_NAME);

			stockCfg.setCacheMode(CacheMode.PARTITIONED); // Default.
			stockCfg.setIndexedTypes(String.class, Stock.class);

			IgniteCache<String, Stock> cache = ignite.getOrCreateCache(stockCfg);
			//            SqlQuery sql = new SqlQuery(Stock.class, "aOpen > ?");
			SqlFieldsQuery sql = new SqlFieldsQuery("select count(*) from Stock where aOpen > ? ");
			//            try (QueryCursor<Entry<Long, Person>> cursor = cache.query(sql.setArgs(1000))) {
			//                for (Entry<Long, Person> e : cursor)
			//                    System.out.println(e.getValue().toString());
			//            }
			QueryCursor<List<?>> cursor = null;
			try {
				cursor = cache.query(sql.setArgs(0.2));
				for (List<?> row : cursor)
					System.out.println("Full name: " + row.get(0));
			} catch (Exception e) {
				cursor.close();
			}
			cache.close();
		}
	}

}
